home *** CD-ROM | disk | FTP | other *** search
/ Unreal Tournament Game Programming for Teens / UnrealTournamentGameProgrammingForTeens.iso / Chapter Files / Chapter08 / UT2004 / Ch08Area / Classes / CommandCodeTrigger.uc < prev    next >
Encoding:
Text File  |  2006-11-01  |  4.0 KB  |  145 lines

  1.  
  2. //=================================================================
  3. // CommandCodeTrigger 
  4. // See CommandCodeTrigger.txt
  5. //=================================================================
  6. class CommandCodeTrigger extends Trigger placeable;
  7.     
  8.     // data members for messages and random numbers
  9.     var private string FirstMessage;
  10.     var private string CodeForSearch;
  11.     const NUMOFMESSAGES = 6;
  12.     enum PState{
  13.            UP,
  14.            DOWN
  15.     }; 
  16.    // #1
  17.    const CODELENGTH = 8;
  18.    const NUMOFTRIES = 20;
  19.    const LOWASCII = 97;
  20.    const HIGHASCII = 122; 
  21.  
  22. function PostBeginPlay()
  23. {
  24.     FirstMessage = "Go!";
  25.     Super.PostBeginPlay();
  26.     Message = FirstMessage;
  27.     // #2
  28.     CodeForSearch = MakeCode(CODELENGTH);
  29. }// end PostBeginPlay()
  30.  
  31. function Touch( actor Other )
  32. {
  33.     if (IsRelevant( Other ) )
  34.     {
  35.         if (Pawn(Other).bIsCrouched){ // down state
  36.             Message= MakeMessage(PState.DOWN);
  37.         }// end if
  38.         else{ // up state
  39.             Message= MakeMessage(PState.UP);
  40.         }// end else
  41.  
  42.         Super.Touch(Other);
  43.     }// end outer if
  44. }// end Touch()
  45.  
  46. private function string MakeMessage(PState state){
  47.     local int RandomNumber;
  48.     local string ActionMessage; 
  49.     RandomNumber = Rand(NUMOFMESSAGES);
  50.  
  51.     // Build messages on the basis of up or down state  
  52.     
  53.     if( state == PState.UP ){
  54.        ActionMessage @= "Get down! ";
  55.  
  56.        // #3
  57.        // Reveal the code
  58.        ActionMessage @= "Here is the code: "; 
  59.        ActionMessage @=  GetCodeForSearch();
  60.     }
  61.     else if( state == PState.DOWN ){
  62.        ActionMessage @= "Get up! - ";
  63.        // Convey a message
  64.        ActionMessage @= GetMessageText(RandomNumber); 
  65.        // find a letter of the code
  66.        // #4
  67.        ActionMessage @=  ReportCodeFound( CodeForSearch  );
  68.     }
  69.     else {
  70.        ActionMessage = "Okay.";
  71.     }
  72.     return ActionMessage; 
  73. }
  74.  
  75. private function string GetMessageText(int index){
  76.    // Define a static array of the string type
  77.    local string PawnMessages[NUMOFMESSAGES]; 
  78.    local string TMessage;
  79.    // Assign text values to elements 
  80.    PawnMessages[0]= "Watch out behind you!";
  81.    PawnMessages[1]= "Turn to your left!";
  82.    PawnMessages[2]= "Get ready to go!";
  83.    PawnMessages[3]= "Did you see the danger?";
  84.    PawnMessages[4]= "Can we move again?";
  85.    PawnMessages[5]= "How many did you see?";
  86.   // Retrieve an element from the array
  87.    if(index < NUMOFMESSAGES && index >= 0){
  88.       TMessage = PawnMessages[index];
  89.    }
  90.    return TMessage;
  91. }
  92.  
  93. private function string MakeCode(int limit){
  94.     local int Ctr;
  95.     local string Code;
  96.    // Control for the while statement 
  97.    Ctr = 0;
  98.    // #5
  99.    while(Ctr < limit){
  100.       // Build a sting using random numbers
  101.       // Convert the numbers to letters
  102.       Code $= Chr(GenerateRandom());
  103.       // Increment the count
  104.       Ctr++;
  105.     }// end while   
  106.     return Code;
  107. }// end MakeCode
  108.  
  109. // #6
  110. private function int GenerateRandom(){
  111.      local float high, low;
  112.      Low = LOWASCII;
  113.      High = HIGHASCII; 
  114.      // Return an integer
  115.      return Int( RandRange( Low , High ));
  116.   }
  117.  
  118. // #7
  119. private function string GetCodeForSearch(){
  120.      return CodeForSearch;
  121. }
  122.  
  123. private function string ReportCodeFound(string Code){
  124.      local string SelectedLetter, Report;
  125.      local int Ctr;
  126.         
  127.      // #8
  128.      for(Ctr = 0; Ctr < NUMOFTRIES; Ctr++){
  129.          // Cast numbers to a string
  130.          SelectedLetter = Chr(GenerateRandom());         
  131.          // See if it is in the string
  132.          // #9
  133.          // Use compound Boolean to determine if
  134.          // the letter is in the code range
  135.          if( InStr(Code, SelectedLetter) >= 0 
  136.              && InStr(Code, SelectedLetter) <= CODELENGTH){
  137.            Report @= "Okay, you have found part of the code: ";
  138.            // Add the letter to the report
  139.            Report @= SelectedLetter; 
  140.            break;        
  141.          }// end if
  142.       }// end for  
  143.       return Report;   
  144. }
  145.